package com.zyz.springbootredis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Key;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/redis")
public class MyController {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/testString")
    public String testString() {
        redisTemplate.opsForValue().set("key1","111");
        redisTemplate.opsForValue().set("key2","222");
        redisTemplate.opsForValue().set("key3","333");

        String value = redisTemplate.opsForValue().get("key1");
        System.out.println("value = " + value);

        return "ok";
    }

    @GetMapping("/testList")
    public String testList() {
        // 清除之前的数
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushDb();
            return null;
        });

        // 尾插
        redisTemplate.opsForList().rightPushAll("key","111","222","333");
        redisTemplate.opsForList().leftPush("key","444");
        redisTemplate.opsForList().rightPop("key");

        // 遍历
        List<String> key = redisTemplate.opsForList().range("key", 0, -1);
        System.out.println("key = " + key);

        return "ok";
    }

    @GetMapping("/testSet")
    public String testSet() {
        redisTemplate.execute(new RedisCallback<Object>() {
            @Override
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
                connection.flushDb();
                return null;
            }
        });
        redisTemplate.opsForSet().add("key1","111","222","333");
        Set<String> members1 = redisTemplate.opsForSet().members("key1");
        System.out.println("members1 = " + members1);

        return "ok";
    }

    @GetMapping("/testHash")
    public String testHash() {
        redisTemplate.execute(new RedisCallback<Object>() {
            @Override
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
                connection.flushDb();
                return null;
            }
        });
        redisTemplate.opsForHash().put("key1","1","111");
        redisTemplate.opsForHash().put("key2","2","222");
        redisTemplate.opsForHash().put("key3","3","333");

        Object value1_1 = redisTemplate.opsForHash().get("key1", "1");
        System.out.println("value1_1 = " + value1_1);

        Object value1_2 = redisTemplate.opsForHash().get("key2", "2");
        System.out.println("value1_2 = " + value1_2);

        return "ok";
    }

    @GetMapping("/testZSet")
    public String testZSet() {
        redisTemplate.execute(new RedisCallback<Object>() {
            @Override
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
                connection.flushDb();
                return null;
            }
        });

        redisTemplate.opsForZSet().add("key","zhangsan",90);
        redisTemplate.opsForZSet().add("key","lisi",86);
        redisTemplate.opsForZSet().add("key","wangwu",93);

        Set<ZSetOperations.TypedTuple<String>> key = redisTemplate.opsForZSet().rangeWithScores("key", 0, -1);
        System.out.println("key = " + key);
        return "ok";
    }
}


